package com.yaozheng.vocationaltraining.service.impl.exam;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.exam.ILimitedChallengeView;
import com.yaozheng.vocationaltraining.service.exam.LimitedChallengeService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

/**
 * Created by Administrator on 2017/9/3.
 */
@EBean
public class LimitedChallengeServiceImpl implements LimitedChallengeService {

    ILimitedChallengeView iLimitedChallengeView;

    @Override
    public void init(ILimitedChallengeView iLimitedChallengeView) {
        this.iLimitedChallengeView = iLimitedChallengeView;
    }

    @Background
    @Override
    public void limitedChallengeList() {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/exam/races",
                    iLimitedChallengeView.getToken(), new BaseView(iLimitedChallengeView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iLimitedChallengeView.limitedChallengeListSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iLimitedChallengeView.limitedChallengeListError(error);
                        }

                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iLimitedChallengeView.isResponseResult()) {
                iLimitedChallengeView.limitedChallengeListError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }
        }
    }
}
